<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if(!isset($_GET['order']))
	header("Location: home.php");

$order = mysql_real_escape_string($_GET['order']);

$booster_result = mysql_query("SELECT * FROM booster_order WHERE order_record = $order");
$booster_row = mysql_fetch_array($booster_result);

if($row['type'] != 2 && $user != $booster_row['booster'])
	header("Location: home.php");

$result = mysql_query("SELECT * FROM order_record WHERE id = $order");
$row = mysql_fetch_array($result);
$summoner = $row['summoner'];
$region = $row['region'];

require_once("unirest/lib/Unirest.php");

$newresponse = Unirest::get(
  "https://teemojson.p.mashape.com/player/".$region."/".$summoner."/leagues",
  array(
	"X-Mashape-Authorization" => "l5ekV2qGSVYpEWLviXHg4A6oRHRtqNBa"
  ),
  null
);

$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues->array[0]->entries->array;
$y = 0;

while(isset($newdecode[$y]))
{
	if($newdecode[$y]->playerOrTeamName == $order_row['summoner'])
	{
		$newwins = $newdecode[$y]->wins;
		$newlosses = $newdecode[$y]->losses;
		break;
	}

	$y++;
}

$ending_stats = $newwins." wins, ".$newlosses." losses";

mysql_query("UPDATE order_record SET complete = 1, ending_stats = '$ending_stats' WHERE id = $order");
mysql_query("UPTADE booster_order SET status = 3 WHERE id = ".$booster_row['id']);

header("Location: home.php");
?>